package com.framework.common.model.response;

import com.framework.common.enums.result.ResultEnum;
import java.io.Serializable;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.common.model.response
 * @description 返回参数对应类
 * @datetime 2019-01-09 11:11:11
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 对应状态码
     */
    private Integer code;
    /**
     * 返回描述
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;

    /**
     * 查询总数
     */
    private Integer count;
    /**
     * 处理时间戳
     */
    private Long timeStamp;

    public static <T> R<T> ok() {
        return restResult(null, ResultEnum.CODE0.getCode(), ResultEnum.CODE0.getMsg(), null);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultEnum.CODE0.getCode(), ResultEnum.CODE0.getMsg(), null);
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, ResultEnum.CODE0.getCode(), msg, null);
    }

    public static <T> R<T> ok(Integer count) {
        return restResult(null, ResultEnum.CODE0.getCode(), ResultEnum.CODE0.getMsg(), count);
    }

    public static <T> R<T> ok(T data, Integer count) {
        return restResult(data, ResultEnum.CODE0.getCode(), ResultEnum.CODE0.getMsg(), count);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ResultEnum.CODE0.getCode(), msg, null);
    }

    public static <T> R<T> ok(T data, ResultEnum resultEnum) {
        return restResult(data, resultEnum.getCode(), resultEnum.getMsg(), null);
    }

    public static <T> R<T> ok(T data, ResultEnum resultEnum, Integer count) {
        return restResult(data, resultEnum.getCode(), resultEnum.getMsg(), count);
    }

    public static <T> R<T> fail() {
        return restResult(null, ResultEnum.CODE_1.getCode(), ResultEnum.CODE_1.getMsg(), null);
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, ResultEnum.CODE_1.getCode(), msg, null);
    }

    public static <T> R<T> fail(Integer code) {
        return restResult(null, code, ResultEnum.CODE_1.getMsg(), null);
    }

    public static <T> R<T> fail(ResultEnum resultEnum) {
        return restResult(null, resultEnum.getCode(), resultEnum.getMsg(), null);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, ResultEnum.CODE_1.getCode(), ResultEnum.CODE_1.getMsg(), null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, ResultEnum.CODE_1.getCode(), msg, null);
    }

    public static <T> R<T> fail(Integer code, String msg) {
        return restResult(null, code, msg, null);
    }

    public static <T> R<T> fail(T data, ResultEnum resultEnum) {
        return restResult(data, resultEnum.getCode(), resultEnum.getMsg(), null);
    }

    public static <T> R<T> fail(T data, Integer code, String msg) {
        return restResult(data, code, msg, null);
    }

    private static <T> R<T> restResult(T data, Integer code, String msg, Integer count) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setTimeStamp(System.currentTimeMillis());
        apiResult.setCount(count);
        return apiResult;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg == null ? null : msg.trim();
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Long timeStamp) {
        this.timeStamp = timeStamp;
    }
}
